package com.xzt.meeting.controller;

import com.xzt.meeting.convert.DateConverter;
import com.xzt.meeting.entity.Meeting;
import com.xzt.meeting.entity.PageEntity;
import com.xzt.meeting.service.MeetingService;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/8 21:23
 * @Version 1.0
 * @Introduce
 */

@WebServlet("/meeting/*")
public class MeetingServlet extends BaseServlet{

    private MeetingService meetingService = new MeetingService();

    public void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,String> param = getParam(req);
        Integer currPage = Integer.parseInt(param.get("currPage"));
        String title = param.get("title");

        // 会议状态(默认为-1)
        Integer status = -1;

        if (!"".equals(param.get("status"))) {
            status = Integer.parseInt(param.get("status"));
        }

        PageEntity<Meeting> meetingList = meetingService.findPage(currPage,title,status);

        req.setAttribute("pageData",meetingList);
        req.setAttribute("title",title);
        req.setAttribute("status",status);

        req.getRequestDispatcher("/html/meeting.jsp").forward(req,resp);

    }

    public void addMeeting(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String> param = getParam(req);

        Meeting meeting = new Meeting();

        ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();

        DateConverter dateConverter = new DateConverter();
        convertUtilsBean.register(dateConverter, Date.class);

        BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtilsBean);

        beanUtils.populate(meeting, param);

        // 去除左右括号
        String makeUser = meeting.getMakeUser();
        if (makeUser.charAt(0)=='[') {
            makeUser = makeUser.substring(1, makeUser.length() - 1);              // [1,2,3,4]--->1,2,3,4] --> 1,2,3,4
            meeting.setMakeUser(makeUser);
        }


//        makeUser = makeUser.substring(1, makeUser.length() - 1);


        meetingService.save(meeting);

        resp.sendRedirect("/meeting/findPage?currPage=1");
    }

    public void meetingDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> param = getParam(req);
        Long meetingId = Long.parseLong(param.get("id"));

        Meeting meeting = meetingService.findMeetingById(meetingId);

        // 需要参加会议的用户
        String[] joinUser = meeting.getMakeUser().split(",");

        // 实到人数
        Integer realCount = meetingService.countJoinMeetingByMeetingId(meetingId);

        // 未到人数
        Integer noCount = joinUser.length - realCount;

        // 查看我是否有参加过这个会议
        Boolean isJoinMeeting = meetingService.isJoinMeeting(loginUser.getId(), meetingId);

        req.setAttribute("meeting", meeting);
        req.setAttribute("joinCount", joinUser.length);
        req.setAttribute("realCount", realCount);
        req.setAttribute("noCount", noCount);
        req.setAttribute("isJoinMeeting", isJoinMeeting);
        req.setAttribute("isJoin", Arrays.asList(joinUser).contains(loginUser.getId()+""));
        System.out.println("可以参加会议的id"+joinUser);
        req.getRequestDispatcher("/html/meeting_detail.jsp").forward(req, resp);
    }

    public void joinMeeting(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> param = getParam(req);

        Long id = Long.parseLong(param.get("id"));

        Boolean isJoinMeeting = Boolean.parseBoolean(param.get("isJoinMeeting"));

        if (isJoinMeeting) {

            meetingService.unJoinMeeting(loginUser.getId(), id);
        } else {

            meetingService.joinMeeting(loginUser.getId(), id);

        }

        resp.sendRedirect("/meeting/meetingDetail?id="+id);
    }
}
